﻿<div id="Content-Body" style="min-width:1000px;">
    <div class="toggle toggle-filter" resource="Commands-Table,Commands-Table-Paging">
        <span class="data-fill" resource="Commands-Table-Paging">共 @Total 条数据</span>

        <button value="'[BA]Sign':15" class="item active">所有命令</button>
        <button value="'[BA]Sign':1" class="item">查询命令</button>
        <button value="'[BA]Sign':2" class="item">增加命令</button>
        <button value="'[BA]Sign':4" class="item">修改命令</button>
        <button value="'[BA]Sign':8" class="item">删除命令</button>
        <button value="'[BA]Sign':128" class="item">触发器命令</button>
    </div>

    <div class="tools">
        <!--<input type="button" class="button right super-search" resource="Commands-Table" value="高级搜索" />-->

        <form class="filter right" resource="Commands-Table,Commands-Table-Paging">
            <input class="left w280 input" type="text" name="[2Fuzzy]Name,[2ORFuzzy]Description" placeholder="搜索名称或说明..." /><input type="submit" class="right button" value="Q" />
        </form>

        <!--<input type="button" class="tool button refresh" resource="Commands-Table" value="刷新"/>-->
        <input type="button" class="tool button insert" resource="Commands-Table" frame="TempCommandFrame" command="InsertCommand" value="新增" />
        <input type="button" class="tool button update" resource="Commands-Table" frame="TempCommandFrame" command="UpdateCommand" value="修改" />
        <input type="button" class="tool button delete" resource="Commands-Table" command="DeleteCommand" value="删除" />
    </div>

    <div class="table-box">
        <table class="table" id="Commands-Table" command="ReadPageCommands" parameters="Begin:1,End:15,'[BA]Sign':15,DefaultOrders:'DId'">
            <thead>
                <tr>
                    <td name="selection" style="width:30px;"><input type="checkbox" /></td>
                    <td class="sort" name="Id" style="width:30px;">命令 ID</td>
                    <td class="sort" name="Type" style="width:30px;">命令类型</td>
                    <td class="sort" name="Sign" style="width:56px;" renderer="return Renderers.commandSign(data.Sign)">命令标识</td>
                    <td class="sort" name="Name" style="width:80px;">命令名称</td>
                    <td class="sort" name="Description" style="width:120px;">命令说明</td>
                    <td class="hidden" name="Code" style="width:180px;">命令代码</td>
                    <td name="operation" style="width:50px;" renderer="return ViewDemoRender(data)">操作</td>
                </tr>
            </thead>
        </table>
    </div>

    <div id="Commands-Table-Paging" command="GetCommandsTotal" resource="Commands-Table">

    </div>

    <form id="TempCommandFrame" text="命令信息" class="frame temp" style="width:1280px;height:800px;margin-left:-640px;margin-top:-400px;" onsubmit="return app.frameSubmit(this)">
        <div class="top">
            <div class="right">
                <span class="min" title="最小化"></span>
                <span class="max" title="最大化"></span>
                <span class="close" title="关闭"></span>
            </div>
            <span class="title">查看/修改命令信息</span>
        </div>
        <div class="content" readonly-where="data.Readonly">
            <div class="content">
                <span class="title">基本信息</span>
                <input type="hidden" name="Id" />
                <div class="item">
                    <label class="text">命令名称：</label>
                    <input verify="notNull" class="input w555" type="text" name="Name" placeholder="命令名称" />
                </div>
                <br />
                <div class="item">
                    <label class="text">命令类型：</label>
                    <select onchange="TypeChanged(this)" class="input w280" name="Type">
                        <option value="T-SQL">T-SQL</option>
                        <option value="C#">C#</option>
                        <option value="System">System</option>
                        <option value="StaticJson">StaticJson</option>
                    </select>
                </div>
                <br />
                <div class="item">
                    <label class="text">命令功能：</label>
                    <input class="radio" name="CommandSign1" id="SignSelect" type="radio" value="1" checked="checked" />
                    <label class="label" for="SignSelect">查询数据</label>
                    <input class="radio ml20" name="CommandSign1" id="SignInsert" type="radio" value="2" />
                    <label class="label" for="SignInsert">添加数据</label>
                    <input class="radio ml20" name="CommandSign1" id="SignUpdate" type="radio" value="4" />
                    <label class="label" for="SignUpdate">修改数据</label>
                    <input class="radio ml20" name="CommandSign1" id="SignDelete" type="radio" value="8" />
                    <label class="label" for="SignDelete">删除数据</label>
                    <!--触发器命令-->
                    <input class="radio ml20 temp" name="CommandSign1" id="SignTrigger" type="radio" value="128" />
                </div>
                <br />
                <div class="item">
                    <label class="text">命令标识：</label>
                    <input class="radio" name="CommandSign2" id="SignAuthorize" type="checkbox" value="16" />
                    <label class="label" for="SignAuthorize" title="用户执行该命令时需要进过授权检查，这将损耗一点性能">需要授权</label>
                    <input class="radio ml20" name="CommandSign2" id="SignNoCall" type="checkbox" value="32" />
                    <label class="label" for="SignNoCall" title="不能被用户所调用，只能在程序中使用">不允许执行</label>
                    <input class="radio ml20" name="CommandSign2" id="SingleObjectResult" type="checkbox" value="64" />
                    <label class="label" for="SingleObjectResult" title="指定返回值是一个对象">单对象返回值</label>
                    <input class="radio ml20" name="CommandSign2" id="MemoryCache" type="checkbox" value="256" />
                    <label class="label" for="MemoryCache" title="使用内存缓存">内存缓存</label>
                    <input class="radio ml20" name="CommandSign2" id="DiskCache" type="checkbox" value="512" />
                    <label class="label" for="DiskCache" title="使用磁盘缓存">磁盘缓存</label>
                </div>
                <br />
                <div class="item">
                    <label class="text">缓存依据：</label>
                    <input class="input w555" type="text" name="CacheToken" placeholder="缓存依据" />
                </div>
                <br />
                <div class="item">
                    <label class="text">命令说明：</label>
                    <input verify="notNull" class="input w555" type="text" name="Description" placeholder="命令说明" />
                </div>
            </div>
            <div id="Params" class="content many" field="CommandId" command="InsertCommandParam">
                <span class="title">
                    <a id="AddParam" onclick="AddParam(this)" class="link" style="float: right">添加字段</a>
                    <a id="AutoImportParameters" onclick="AutoImportParameters(this)" class="link" style="float: right; margin-right:10px;">自动识别</a>
                    参数
                </span>
            </div>
            <div id="Results" class="content many" field="CommandId" command="InsertCommandResult">
                <span class="title">
                    <a id="AddResult" onclick="AddResult(this)" class="link" style="float: right">添加字段</a>
                    返回值
                </span>
            </div>
            <div class="content">
                <span class="title">代码</span>
                <textarea class="fit input textarea" verify="notNull" name="Code" placeholder="命令代码..." style="width: auto; border: none; resize: vertical; min-height: 380px;"></textarea>
            </div>
            <div class="content">
                <span class="title">示例文本</span>
                <textarea class="fit input textarea" name="Demo" placeholder="示例文本..." style="width: auto; border: none; resize: vertical; min-height: 380px;"></textarea>
            </div>
        </div>
        <div class="bottom">
            <input type="submit" class="button" value="保存修改" display-where="!data.Readonly" />
            <input type="button" class="button close" value="返回" />
        </div>
    </form>

    <form id="TempViewDemoFrame" text="命令示例" class="frame temp" style="width:1280px;height:800px;margin-left:-640px;margin-top:-400px;" onsubmit="return app.frameSubmit(this)">
        <div class="top">
            <div class="right">
                <span class="min" title="最小化"></span>
                <span class="max" title="最大化"></span>
                <span class="close" title="关闭"></span>
            </div>
            <span class="title">命令示例</span>
        </div>
        <div class="content">
            <div class="content" readonly-where="true">
                <span class="title">基本信息</span>
                <input type="hidden" name="Id" />
                <div class="item">
                    <label class="text">命令名称：</label>
                    <input verify="notNull" class="input w555" type="text" name="Name" placeholder="命令名称" />
                </div>
                <br />
                <div class="item">
                    <label class="text">命令功能：</label>
                    <input class="radio" name="CommandSign1" id="SignSelect" type="radio" value="1" checked="checked" />
                    <label class="label" for="SignSelect">查询数据</label>
                    <input class="radio ml20" name="CommandSign1" id="SignInsert" type="radio" value="2" />
                    <label class="label" for="SignInsert">添加数据</label>
                    <input class="radio ml20" name="CommandSign1" id="SignUpdate" type="radio" value="4" />
                    <label class="label" for="SignUpdate">修改数据</label>
                    <input class="radio ml20" name="CommandSign1" id="SignDelete" type="radio" value="8" />
                    <label class="label" for="SignDelete">删除数据</label>
                </div>
                <br />
                <div class="item">
                    <label class="text">命令标识：</label>
                    <input class="radio" name="CommandSign2" id="SignAuthorize" type="checkbox" value="16" />
                    <label class="label" for="SignAuthorize" title="用户执行该命令时需要进过授权检查，这将损耗一点性能">需要授权</label>
                    <input class="radio ml20" name="CommandSign2" id="SignNoCall" type="checkbox" value="32" />
                    <label class="label" for="SignNoCall" title="不能被用户所调用，只能在程序中使用">不允许执行</label>
                    <input class="radio ml20" name="CommandSign2" id="SingleObjectResult" type="checkbox" value="64" />
                    <label class="label" for="SingleObjectResult" title="指定返回值是一个对象">单对象返回值</label>
                    <input class="radio ml20" name="CommandSign2" id="SignFilters" type="checkbox" value="1024" />
                    <label class="label" for="SignFilters" title="支持使用 [E]FieldName 进行数据筛选">支持筛选</label>
                    <input class="radio ml20" name="CommandSign2" id="SignOrders" type="checkbox" value="2048" />
                    <label class="label" for="SignOrders" title="支持指定 Orders:AId 进行数据排序">支持排序</label>
                </div>
                <br />
                <div class="item">
                    <label class="text">命令说明：</label>
                    <input verify="notNull" class="input w555" type="text" name="Description" placeholder="命令说明" />
                </div>
            </div>
            <div id="Params" class="content">
                <span class="title">
                    <a id="AddParam" onclick="AddDemoParam(this)" class="link" style="float: right">添加字段</a>
                    参数
                </span>
                <div style="text-align:center;">
                    <input class="radio" id="WithLoginParams" type="checkbox" checked="checked" />
                    <label class="label" for="WithLoginParams" title="上传登陆信息参数">上传登陆信息参数</label>
                </div>
            </div>
            <div id="Results" class="content">
                <span class="title">
                    <a id="AddResult" onclick="AddDemoResult(this)" class="link" style="float: right;display:none;">添加字段</a>
                    返回值
                </span>
            </div>
            <div class="content">
                <span class="title">返回结果</span>
                <textarea class="fit input textarea" name="ResponseText" placeholder="返回结果..." style="width: auto; border: none; resize: vertical; min-height: 380px;"></textarea>
            </div>
            <div class="content">
                <span class="title">示例文本</span>
                <textarea disabled="disabled" class="fit input textarea" name="Demo" placeholder="示例文本..." style="width: auto; border: none; resize: vertical; min-height: 380px;"></textarea>
            </div>
        </div>
        <div class="bottom">
            <input type="button" class="button" value="Submit" onclick="return DemoSubmit(this)" />
            <input type="button" class="button close" value="返回" />
        </div>
    </form>

    <div id="DemoParamItem" class="item temp">
        <div readonly-where="data.Type!=0">
            <label class="text">参数：</label>
            <select disabled="disabled" class="input w100" name="Type">
                <option value="32">String</option>
                <option value="1">Boolean</option>
                <option value="2">Int</option>
                <option value="4">Long</option>
                <option value="8">Float</option>
                <option value="16">Double</option>
                <option value="2048">Decimal</option>
                <option value="64">DateTime</option>
                <option value="128">TimeSpan</option>
                <option value="256">Guid</option>
                <option value="512">Object</option>
                <option value="1024">Ids</option>
            </select>
            <span class="space"></span>
            <span class="s-allow-null" style="color:red;visibility:hidden;">*</span>
            <input verify="notNull" type="text" class="input w180" name="Name" placeholder="参数名称" />
            <span class="space"></span>
            <input disabled="disabled" verify="notNull" type="text" class="input w374" name="Description" placeholder="参数说明" />
            <span class="space"></span>
            <div class="inline-block" readonly-where="false">
                <input type="text" class="input w180" name="Demo" placeholder="值" />
            </div>
            <a class="delete" onclick="RemoveParam(this)"></a>
        </div>
    </div>

    <div id="DemoResultItem" class="item temp">
        <div readonly-where="true">
            <label class="text">返回值：</label>
            <select class="input w100" name="Type">
                <option value="1">Boolean</option>
                <option value="2">Int</option>
                <option value="4">Long</option>
                <option value="8">Float</option>
                <option value="16">Double</option>
                <option value="2048">Decimal</option>
                <option value="32">String</option>
                <option value="64">DateTime</option>
                <option value="128">TimeSpan</option>
                <option value="256">Guid</option>
                <option value="512">Object</option>
                <option value="1024">Ids</option>
            </select>
            <span class="space"></span>
            <span style="color:red;visibility:hidden;">*</span>
            <input verify="notNull" type="text" class="input w180" name="Name" placeholder="返回值名称" />
            <span class="space"></span>
            <input verify="notNull" type="text" class="input w374" name="Description" placeholder="返回值说明" />
            <span class="space"></span>
            <input type="text" class="input w180" name="Demo" placeholder="示例值" />
        </div>
    </div>

    <div id="TempParamItem" class="item temp">
        <label class="text">参数：</label>
        <select class="input w100" name="Type">
            <option value="1">Boolean</option>
            <option value="2">Int</option>
            <option value="4">Long</option>
            <option value="8">Float</option>
            <option value="16">Double</option>
            <option value="2048">Decimal</option>
            <option value="32">String</option>
            <option value="64">DateTime</option>
            <option value="128">TimeSpan</option>
            <option value="256">Guid</option>
            <option value="512">Object</option>
            <option value="1024">Ids</option>
        </select>
        <span class="space"></span>
        <input class="radio" type="checkbox" id="SystemParam" name="Sign" value="2" />
        <label for="SystemParam" class="label">系统参数</label>
        <input class="radio ml20" type="checkbox" id="AllowNull" name="Sign" value="1" />
        <label for="AllowNull" class="label">允许为 Null</label>
        <span class="space"></span>
        <input verify="notNull" type="text" class="input w150" name="Name" placeholder="参数名称" />
        <span class="space"></span>
        <input verify="notNull" type="text" class="input w180" name="Description" placeholder="参数说明" />
        <span class="space"></span>
        <input type="text" class="input w180" name="Demo" placeholder="示例值" />
        <a class="delete" onclick="RemoveParam(this)"></a>
    </div>

    <div id="TempResultItem" class="item temp">
        <label class="text">返回值：</label>
        <select class="input w100" name="Type">
            <option value="1">Boolean</option>
            <option value="2">Int</option>
            <option value="4">Long</option>
            <option value="8">Float</option>
            <option value="16">Double</option>
            <option value="2048">Decimal</option>
            <option value="32">String</option>
            <option value="64">DateTime</option>
            <option value="128">TimeSpan</option>
            <option value="256">Guid</option>
            <option value="512">Object</option>
            <option value="1024">Ids</option>
        </select>
        <span class="space"></span>
        <input verify="notNull" type="text" class="input w180" name="Name" placeholder="返回值名称" />
        <span class="space"></span>
        <input verify="notNull" type="text" class="input w374" name="Description" placeholder="返回值说明" />
        <span class="space"></span>
        <input type="text" class="input w180" name="Demo" placeholder="示例值" />
        <a class="delete" onclick="RemoveResult(this)"></a>
    </div>

    <script>
        var ParameterInfos = {
        };

        $.ajax({
            url: "/json/parameterInfos.json",
            data: {
                version: app.version
            },
            dataType: "json",
            success: function (r) {
                ParameterInfos = r;
            }
        });

        $("#TempCommandFrame").on("process", function (e, Data) {
            var Sign = 0;

            Sign |= parseInt(Data.CommandSign1);

            var CommandSign2 = Data.CommandSign2 ? Data.CommandSign2 instanceof Array ? Data.CommandSign2 : [Data.CommandSign2] : [];

            for (var i = 0; i < CommandSign2.length; i++) {
                Sign |= parseInt(CommandSign2[i]);
            }

            Data.Sign = Sign;

            delete Data.CommandSign1;
            delete Data.CommandSign2;
        });

        $("#TempCommandFrame,#TempViewDemoFrame").on("apply", function (e, Data) {
            if (e.currentTarget != e.target) {
                return;
            }

            var Me = $(this);

            var Sign = Data.Sign;

            if ((Sign & 1) != 0) {
                Data.CommandSign1 = 1;
            } else if ((Sign & 2) != 0) {
                Data.CommandSign1 = 2;
            } else if ((Sign & 4) != 0) {
                Data.CommandSign1 = 4;
            } else if ((Sign & 8) != 0) {
                Data.CommandSign1 = 8;
            } else if ((Sign & 128) != 0) {
                Data.CommandSign1 = 128;

                Me.find("[name=CommandSign1]").attr("disabled", true)
            }

            var Sign2 = Data.CommandSign2 = [];

            if ((Sign & 16) != 0) {
                Sign2.push(16);
            }
            if ((Sign & 32) != 0) {
                Sign2.push(32);
            }
            if ((Sign & 64) != 0) {
                Sign2.push(64);
            }
            if ((Sign & 256) != 0) {
                Sign2.push(256);
            }
            if ((Sign & 512) != 0) {
                Sign2.push(512);
            }

            function ApplyToParameterInfos(Result) {
                for (var i = 0; i < Result.length; i++) {
                    ParameterInfos[Result[i].Name] = Result[i];
                }
            }

            app.server({ Command: "ReadAllCommandResultsByCommandId", CommandId: Data.Id }, function (Result) {

                ApplyToParameterInfos(Result);

                var AddResultButton = Me.find("#AddResult");

                for (var i = 0; i < Result.length; i++) {
                    AddResultButton.click();
                }

                var ResultItems = Me.find("#Results .item");

                for (var i = 0; i < Result.length; i++) {
                    app.apply($(ResultItems[i]), Result[i]);
                }
            })

            app.server({ Command: "ReadAllCommandParamsByCommandId", CommandId: Data.Id }, function (Result) {

                ApplyToParameterInfos(Result);

                var AddParamButton = Me.find("#AddParam");

                for (var i = 0; i < Result.length; i++) {
                    AddParamButton.click();
                }

                var ParamItems = Me.find("#Params .item");

                for (var i = 0; i < Result.length; i++) {
                    var Sign = [];

                    if ((Result[i].Sign & 1) != 0) {
                        Sign.push(1);
                    }
                    if ((Result[i].Sign & 2) != 0) {
                        Sign.push(2);
                    }

                    Result[i].Sign = Sign;

                    app.apply($(ParamItems[i]), Result[i]);
                }
            });
        });

        $("#DemoParamItem").on("apply", function (e, Data) {
            var Frame = $(this).parents(".frame:first");

            if (Data.Name == "$Filters") {
                Frame.find("#SignFilters").prop("checked", true);
            }
            if (Data.Name == "$Orders") {
                Frame.find("#SignOrders").prop("checked", true);
            }
            if (Data.Name.startsWith("$") || Data.Name.startsWith("@")) {
                Data.Name = Data.Name.substr(1);
            }
            if ((Data.Sign & 2) != 0) {
                $(this).remove();
            }
            if ((Data.Sign & 1) == 0) {
                $(this).find(".s-allow-null").css("visibility", "visible");
            }
        });

        $("#TempParamItem").on("process", function (e, Data) {
            var Sign = 0;

            var Signs = Data.Sign ? Data.Sign instanceof Array ? Data.Sign : [Data.Sign] : [];

            for (var i = 0; i < Signs.length; i++) {
                Sign |= parseInt(Signs[i]);
            }

            Data.Sign = Sign;
        });

        $("#TempResultItem").on("process", function (e, Data) {
            Data.Sign = 0;
        });

        $.extend(window, {
            AddParam: function (Button) {
                var Content = $(Button).parents(".content:first");

                var Item = $("#TempParamItem").clone(true);

                Item.removeAttr("id");
                Item.removeClass("temp");

                Content.append(Item);

                this.SortParam(Content, "参数");

                return Item;
            },
            SortParam: function (Content, Name) {
                var Labels = Content.find(".item .text");

                for (var i = 0; i < Labels.length; i++) {
                    $(Labels[i]).html(Name + " " + (i + 1) + "：");
                }
            },
            RemoveParam: function (Button) {
                var Content = $(Button).parents(".content:first");

                var Item = $(Button).parents(".item:first");

                Item.remove();

                this.SortParam(Content, "参数");
            },
            AddResult: function (Button) {
                var Content = $(Button).parents(".content:first");

                var Item = $("#TempResultItem").clone(true);

                Item.removeAttr("id");
                Item.removeClass("temp");

                Content.append(Item);

                this.SortParam(Content, "返回值");

                return Item;
            },
            AddDemoParam: function (Button) {
                var Content = $(Button).parents(".content:first");

                var Item = $("#DemoParamItem").clone(true);

                Item.removeAttr("id");
                Item.removeClass("temp");

                Content.append(Item);

                this.SortParam(Content, "参数");

                return Item;
            },
            AddDemoResult: function (Button) {
                var Content = $(Button).parents(".content:first");

                var Item = $("#DemoResultItem").clone(true);

                Item.removeAttr("id");
                Item.removeClass("temp");

                Content.append(Item);

                this.SortParam(Content, "返回值");

                return Item;
            },
            RemoveResult: function (Button) {
                var Content = $(Button).parents(".content:first");

                var Item = $(Button).parents(".item:first");

                Item.remove();

                this.SortParam(Content, "返回值");
            },
            TypeChanged: function (Select) {
                if (Select.value == "T-SQL") {
                    $(Select).parents(".frame:first").find("#SingleObjectResult").prop("checked", false);
                } else {
                    $(Select).parents(".frame:first").find("#SingleObjectResult").prop("checked", true);
                }
            },
            AutoImportTSQLParametrts: function (Button, Frame) {
                var Code = Frame.find("[name=Code]:first").val();
                var CodeUpper = Code.toUpperCase();

                var ParameterRegex = /(([@$])([A-Za-z_]+[A-Za-z0-9_]*))/g;

                var Names = {};

                for (var Match = ParameterRegex.exec(Code); Match != null; Match = ParameterRegex.exec(Code)) {
                    // Match[1] @Name
                    // Match[2] @
                    // Match[3] Name

                    var Name = Match[3];

                    if (Match[2] == "$") {
                        Name = Match[1];
                    }

                    Names[Name] = { Before: Match[2] };
                }

                for (var Name in Names) {
                    if (Names[Name].Before === "@") {
                        /* 如果是代码中定义的变量，则移除。 */
                        if (CodeUpper.match(RegExp("DECLARE[ \n\r\t]+@" + Name.toUpperCase()))) {
                            delete Names[Name];
                        }
                    }
                }

                Frame.find("#Params input[name=Name]").each(function () {
                    /* 如果参数列表中已经定义，则移除。 */
                    delete Names[$(this).val()];
                });

                for (var Name in Names) {
                    /* 如果参数信息定义中有这个参数信息，则应用数据。 */
                    Names[Name] = $.extend({ Name: Name }, ParameterInfos[Name])
                }

                /* 添加参数信息到参数列表 */
                for (var Name in Names) {

                    var Item = AddParam(Button);

                    app.apply(Item, Names[Name]);
                }

                app.msg("导入 T-SQL 参数完成。");
            },
            AutoImportParameters: function (Button) {
                var Frame = $(Button).parents(".frame:first");

                var Type = Frame.find("[name=Type]:first").val();

                switch (Type) {
                    case "T-SQL":
                        AutoImportTSQLParametrts(Button, Frame);
                        break;
                    default:
                        app.errMsg("该命令类型不支持导入参数。");
                        break;
                }

                var CommandName = Frame.find("input[name=Name]:first").val();
                var Id = Frame.find("input[name=Id]:first").val();

                /* 如果是已经保存的命令，就可以获取返回值列表了。 */
                if (Id && CommandName) {
                    app.server({ Command: "AutoImportResults", CommandName: CommandName }, function (Result) {
                        if (Result.Names) {

                            var Names = {};

                            for (var i = 0; i < Result.Names.length; i++) {
                                var Name = Result.Names[i];

                                Names[Name] = $.extend({ Name: Name }, ParameterInfos[Name]);
                            }

                            Frame.find("#Results input[name=Name]").each(function () {
                                delete Names[$(this).val()];
                            });

                            var AddResultButton = Frame.find("#AddResult");

                            for (var Name in Names) {
                                var Item = AddResult(AddResultButton);

                                app.apply(Item, Names[Name]);
                            }
                        }

                        if (Result[Consts.exception]) {
                            app.errMsg(Result[Consts.exception]);
                        } else {
                            app.msg(Result[Consts.msg]);
                        }

                    });
                }
            },
            ViewDemoRender: function (Data) {
                var Button = $("<a class=\"link\">查看</a>");

                Button.click(function () {
                    var Frame = app.show($("#TempViewDemoFrame"));

                    app.apply(Frame, Data);
                });

                return Button;
            },
            DemoSubmit: function (Button) {
                var Frame = $(Button).parents(".frame:first");
                var Command = Frame.find("[name=Name]:first").val();
                var Params = Frame.find("#Params .item");

                var Data = { Command: Command, __IsDemoRequest: true };

                Params.each(function () {
                    var Name = $(this).find("[name=Name]").val();
                    var Value = $(this).find("[name=Demo]").val();
                    var Type = $(this).find("[name=Type]").val();

                    switch (parseInt(Type)) {
                        case 1:
                            Value = Boolean(Value);
                            break;
                        case 2:
                        case 4:
                        case 8:
                        case 16:
                        case 2048:
                            Value = Number(Value);
                            break;
                    }

                    Data[Name] = Value;
                });

                if (!Frame.find("#WithLoginParams").prop("checked")) {
                    Data.UserId = app.serverRemove;
                    Data.Token = app.serverRemove;
                    Data.UserName = app.serverRemove;
                }

                app.server(Data, function (Result) {
                    if (Result.__Code) {
                        Result[Consts.code] = Result.__Code;
                        delete Result.__Code;
                    }

                    if (Result[Consts.code]) {
                        app.errMsg(Command + " 执行出现错误！");
                    } else {
                        app.msg(Command + " 执行成功！");
                    }

                    Frame.find("[name=ResponseText]").focus();

                    var Text = JSON.stringify(Result, " ", "    ");

                    Frame.find("[name=ResponseText]").val(Text);
                });

                return false;
            }
        });

        if (window.__IsBindCommandDemoServerAfter === undefined) {

            $(app).bind("serverAfter", function (e, Data, Result) {
                if (Data.__IsDemoRequest === true && Result[Consts.code]) {
                    Result.__Code = Result[Consts.code];

                    Result[Consts.code] = 0;
                }
            });

            window.__IsBindCommandDemoServerAfter = true;
        }

        app.control($("#Commands-Table"), "table");
        app.control($("#Commands-Table-Paging"), "paging");
        app.control($("#Content-Body"), "content");
    </script>
</div>